package com.zjz.studynotes;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;

import com.zjz.studynotes.bean.StudyNoteInfo;
import com.zjz.studynotes.db.StudyNotesDb;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 编辑，新增笔记页面
 * Author: zjz
 * Date:2025/5/5 13:06
 */
public class AddActivity extends ComponentActivity {
    private EditText mEtTitle;
    private EditText mEtContent;
    private EditText mEtEvaluate;
    private Button mBtnAdd;
    private Button mBtnCancel;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);



        mEtTitle = findViewById(R.id.et_title);
        mEtContent = findViewById(R.id.et_content);
        mEtEvaluate = findViewById(R.id.et_evaluate);
        mBtnAdd = findViewById(R.id.btn_save);
        mBtnCancel = findViewById(R.id.btn_cancel);

        mBtnCancel.setOnClickListener(view -> finish());
        mBtnAdd.setOnClickListener(view -> save());

    }

    /**
     * 保存校验笔记数据，存入数据库
     * @param
     */
    private void save() {
        if (TextUtils.isEmpty(mEtTitle.getText().toString())) {
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mEtContent.getText().toString())) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        }


        StudyNoteInfo info = new StudyNoteInfo();

        info.setTitle(mEtTitle.getText().toString());
        info.setContent(mEtContent.getText().toString());

        if (!TextUtils.isEmpty(mEtEvaluate.getText().toString())) {
            List evaluates = new ArrayList<String>();
            evaluates.add(mEtEvaluate.getText().toString());
            info.setEvaluate(evaluates);
        }

        StudyNotesDb.getInstance(this).getStudyNoteInfoDao().insert(info);
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }
}
